#version 130

uniform mat4 modelViewMatrix;
uniform mat4 projectionMatrix;

uniform sampler2D vegetationTexture;
uniform sampler2D noiseTexture;
uniform vec3 sunPosition;
uniform vec3 playerPosition;

in vec3 vPosition;
in vec2 vTexcoord;
in vec3 vNormal;

out vec3 fWorldPos;
out vec2 fTexcoord;
out vec3 fNormal;
out vec4 fColor;
out float fAlpha;

void main()
{		
	fWorldPos = vPosition;	
	fTexcoord = vTexcoord;
	fNormal = vNormal;
	
	vec4 diffuse = vec4(1.0, 1.0, 1.0, 1.0);
	vec4 ambient = vec4(0.2, 0.2, 0.2, 1.0);
	
	vec3 lightDir = normalize(sunPosition - fWorldPos);
	float NdotL = max(dot(fNormal, lightDir), 0.0);
	
	vec3 corePosition = vec3(0.0f, 0.0f, 0.0f);  // change to uniform
	vec3 upFromCore = normalize(fWorldPos - corePosition);
	vec3 positionToSun = normalize(sunPosition - fWorldPos);
	float seeSun = dot(positionToSun, upFromCore);
	
	float C = 0.2;	 
	seeSun = clamp(seeSun, 0, C);
	float intensity = 1.0f;
	intensity = seeSun / C;
	fColor = intensity * (NdotL * diffuse) + ambient;
	
	float fadeStart = 15;
	float fadeEnd = 30;
	float dist = length(playerPosition - vPosition);
	fAlpha = 1 - clamp((dist - fadeStart)/(fadeEnd - fadeStart), 0, 1);
	
	gl_Position = projectionMatrix * modelViewMatrix * vec4(vPosition, 1);
}